<?php
/**
 * Created by PhpStorm.
 * User: chenxiaogang
 * Date: 2017/11/10
 * Time: 上午11:34
 */

namespace User\Model;


use Common\Model\CommonModel;

/**
 * Class OrdersSub
 * @package     User\Model
 * @description 子订单表
 * @end
 */
class OrdersSub extends CommonModel
{
    protected $connection = 'DB_CONFIG2';
    protected $trueTableName = 'orders_sub';

    /**
     * @use          [查询用户当前逾期金额]
     * @author       chenxiaogang
     * @param $user_id
     */
    public function getCurrentOverdueAmt($user_id)
    {
        $ordersModel = new Orders();
        //查询最新一笔订单的当前逾期天数
        $st = $ordersModel->alias( 'o' )
            ->field( [
                'o.amount', 'o.interest', 'o.examine', 'o.consulting',
                'current_date()-max(ob.endtime)' => 'current_overdue_days',
            ] )
            ->join( 'orders_sub ob on ob.pid=o.id', 'left' )
            ->where( [ 'ob.user_id' => 14, 'ob.status' => 3 ] )
            ->order( 'o.id DESC' )
            ->find();
        //查询额度配置
        $system_amount = xu_amount( [
            [
                'amount' => $st['amount'],
                'interest' => $st['interest'],
                'examine' => $st['examine'],
                'consulting' => $st['consulting'],
            ],
        ] );
        $current_overdue_amt = $system_amount['overdue_fee'] * $st['current_overdue_days'];
        return $current_overdue_amt;
    }

}